<script setup>
import {onBeforeMount, reactive, ref} from "vue";
import request from "@/utils/request";
import {md5} from "js-md5";
//表单数据
let form=reactive({
  username:'',
  password:'',
  phoneNumber:'',
  registerDate:""
})
//表单规则
const rules = reactive({
  username:[
    {min: 1, max: 16, message: '用户名长度为1~16', trigger: 'blur' },
    { required: true, message: '用户名不能为空', trigger: 'blur' }
  ],
  password:[
    {min: 6, max: 16, message: '密码长度为6~16', trigger: 'blur' },
    {required: true, message: '密码不能为空', trigger: 'blur' }
  ],
  phoneNumber:[
    {min: 11, max: 11, message: '手机号码为11位', trigger: 'blur' },
    { required: true, message: '手机号不能为空', trigger: 'blur' }
  ]
})
//注册接口地址
let registerUrl='http://localhost:8090/register/submit'
//表单实例
const ruleFormRef = ref()
function register(formEl){
  if (!formEl) return
  formEl.validate((valid, fields) => {
    if (valid) {
      let params={
        username:form.username,
        password:md5(form.password),
        phoneNumber:form.phoneNumber,
        registerDate:getDateString(),
        province:"辽宁省"
      }
      request.post(registerUrl,params).then((res)=>{
        alert(res.data.msg)
        if(res.data.code === 200)formEl.resetFields()
      }).catch((err)=>{
        console.log(err)
      })
    }
  })

}
function getDateString(){
  let date = new Date();
  let year = date.getFullYear();
  let month = date.getMonth() + 1;
  let day = date.getDate();
  let Hours = date.getHours();
  let Minutes = date.getMinutes();
  let Seconds = date.getSeconds();
  if (month < 10) {
    month = "0" + month;
  }
  if (day < 10) {
    day = "0" + day;
  }
  if (Hours < 10) {
    Hours = "0" + Hours;
  }
  if (Minutes < 10) {
    Minutes = "0" + Minutes;
  }
  if (Seconds < 10) {
    Seconds = "0" + Seconds;
  }
  return year + '-' + month + '-' + day + ' ' + Hours + ':' + Minutes + ':' + Seconds;

}


</script>

<template>
  <div class="page">
    <div class="container">
      <div class="top">
        <el-avatar :size="80" src="https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png" style="margin-top: 5px"/>
      </div>
      <div class="center">
        <div class="form">
          <el-form :model="form" :rules="rules" ref="ruleFormRef" status-icon>
            <el-form-item prop="username">
              <el-input v-model="form.username" placeholder="注册用户名"></el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input v-model="form.password" placeholder="注册密码" type="password"></el-input>
            </el-form-item>
            <el-form-item prop="phoneNumber">
              <el-input v-model="form.phoneNumber" placeholder="注册手机号"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button @click="register(ruleFormRef)" type="primary">注册</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
      <div class="bottom">
        <a href="/login">立刻登录</a>
      </div>
    </div>
  </div>

</template>

<style scoped>
.page {
  padding-top: 100px;
  width: 100%;
  height: 100%;
  background:url("../../../public/image/loginbg.webp") no-repeat;
  background-size: cover;
}

.container {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  width: 400px;
  height: 400px;
  background-color: rgba(1,1,1,.2);
  margin: 0 auto;
  border: #e9eef3 solid 1px;
  border-radius: 20px;
}

.top {
  width: 100%;
  height: 20%;
}

.center {
  width: 100%;
  height: 70%;
}

.bottom {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  padding-left: 10px;
  width: 95%;
  height: 10%;
}

.el-form-item {
  width: 80%;
  height: 35px;
  margin: 20px auto;
}

.el-radio-group {
  margin: 0 auto;
  color: white;
}


.el-button {
  margin: 0 auto;
  width: 80%;
  border-radius: 20px;
}

a {
  text-decoration: none;
  color: white;
}
a:hover {
  color: skyblue;
}

</style>